using System;
using Common;
using Entity;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using Utils;

namespace CardGameObject.CollectScene
{
    public class CollectCard : MonoBehaviour
    {
        // Card本身属性
        private CollectManager collectManager;

        // Card展示属性
        public TextMeshPro AttackText;
        public TextMeshPro CostText;
        public TextMeshPro HealthText;
        public TextMeshPro NameText;
        public TextMeshPro CountText;
        public SpriteRenderer CardImage;
        public SpriteRenderer CardBack;
        public SpriteRenderer CardRare;

        public long CardId { get; set; }

        public string CardName { get; set; }

        public int Attack { get; set; }

        public int Health { get; set; }

        public CardTypeEnum CardType { get; set; }

        public int Cost { get; set; }

        public CardRarityEnum CardRarity { get; set; }

        public CollectCardVO Card { get; set; }

        public string[] EffectWord { get; set; }

        public string EffectText { get; set; }

        public string[] NameWord { get; set; }

        public int CardCount { get; set; }

        private void Start()
        {
            collectManager = CollectManager.GetInstance();
        }

        private void OnMouseOver()
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (collectManager.IsAddable(CardId)&&CardCount>0)
                {
                    CardCount -= 1;
                    CountText.text = CardCount.ToString();
                    collectManager.AddCardToDeck(CardId, CardName);
                }
            }else if (Input.GetMouseButtonDown(1))
            {
                collectManager.ShowCardDetail(Card);
            }
        }

        public void SetCardInfo()
        {
            if (Card != null)
            {
                BeanUtil.CopyProperties(Card, this);
            }

            AttackText.text = Attack.ToString();
            CostText.text = Cost.ToString();
            HealthText.text = Health.ToString();
            NameText.text = CardName;
            CountText.text = CardCount.ToString();

            EffectWord = JsonConvert.DeserializeObject<string[]>(Card.EffectWord);
            NameWord = JsonConvert.DeserializeObject<string[]>(Card.NameWord);

            Sprite sprite = Resources.Load<Sprite>($"Images/CardImage/Content/{CardName}");
            if (sprite != null)
            {
                CardImage.sprite = sprite;
            }

            // 根据 cardRarity 属性的值更改 CardRare 对应的图像颜色
            switch (CardRarity)
            {
                case CardRarityEnum.N:
                    CardRare.color = Color.white;
                    break;
                case CardRarityEnum.R:
                    CardRare.color = Color.blue;
                    break;
                case CardRarityEnum.SR:
                    CardRare.color = new Color(1f, 0.84f, 0f); // 金黄色
                    break;
                case CardRarityEnum.UR:
                    CardRare.color = Color.red;
                    break;
                default:
                    Debug.LogWarning($"未处理的卡牌稀有度: {CardRarity}");
                    break;
            }
        }
    }
}